package com.heima.bos.web.action.impl;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.transaction.annotation.Transactional;

import com.heima.bos.dao.impl.UserDao;
import com.heima.bos.service.BaseService;
import com.heima.bos.service.UserService;
import com.heima.bos.utils.PageBean;
import com.heima.bos.web.action.ActionInter;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import net.sf.json.util.PropertyFilter;
/**
 * BaseAction
 * @author Administrator
 * @param <T>
 */
@SuppressWarnings("all")
public abstract class BaseAction<T> extends ActionSupport implements ActionInter<T>, ModelDriven<T> {
	/**
	 * 属性封装
	 */
	protected PageBean<T> pageBean = new PageBean<T>();
	
	protected T entity;//模型
	protected abstract BaseService<T> getService();
	
	public BaseAction(){
		//初始化entity
		try {
			ParameterizedType type = null;
			Type typeP = this.getClass().getGenericSuperclass();
			if(typeP instanceof ParameterizedType){
				type = (ParameterizedType) typeP;
			}else{
				type = (ParameterizedType) this.getClass().getSuperclass().getGenericSuperclass();
			}
			Type[] types = type.getActualTypeArguments();
			Class clazz = (Class) types[0];
			DetachedCriteria detachedCriteria = DetachedCriteria.forClass(clazz);
			pageBean.setDetachedCriteria(detachedCriteria);
			entity = (T) clazz.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 分页查询数据
	 */
	public void pageList(){
			this.getService().pageList(pageBean);
	}
	/**
	 * 将对象转为json格式数据回写页面
	 */
	public void writePageBean2Json(PageBean<T> pageBean , String[] excludes){
		
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(excludes);

		JSONObject jsonObject = JSONObject.fromObject(pageBean, jsonConfig);
		String json = jsonObject.toString();

		writeJsonBack(json);
	}
	/**
	 * 将list转为json格式数据
	 * @param list
	 * @param excludes
	 */
	public void writeList2Json(List list , String[] excludes){
		
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(excludes);

		JSONArray array = JSONArray.fromObject(list, jsonConfig);
		String json = array.toString();
		writeJsonBack(json);
	}
	/**
	 * 将Object对象转为json格式数据
	 * @param obj
	 * @param excludes
	 */
	public void writeObject2Json(Object obj , String[] excludes){

		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(excludes);

		JSONObject jsonObject = JSONObject.fromObject(obj);
		String json = jsonObject.toString();
			
		writeJsonBack(json);
	}
	/**
	 * 回写json数据
	 * @param json
	 * @throws IOException 
	 */
	public void writeJsonBack(String json){
		try {
			System.out.println(json);
			ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public String findAll() {
		this.getService().findAll();
		return "list";
	}
	/*
	 * 保存对象
	 */
	public String save() {
		this.getService().save(entity);
		return "addSuccess";
	}
	/**
	 * 更新对象
	 */
	public String update() {
		this.getService().update(entity);
		return "updateSuccess";		
	}

	public String delete() {
		return "";		
	}

	public String findById() {
		return "";		
	}

	public String findByCriteria() {
		return "";
	}

	public String findByNameQuery() {
		return "";
	}

	public String executeNameQuery() {
		return "";		
	}
	//模型驱动封装表单数据
	public T getModel() {
		return entity;
	}
	public void setPage(Integer page) {
		pageBean.setPage(page);;
	}
	public void setRows(Integer rows) {
		pageBean.setPageSize(rows);
	}
}
